package com.hengpeng.itfints.controller.repayment;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.persist.ReceivePlan;
import com.hengpeng.itfinbase.persist.RepaymentPlan;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.itfints.controller.BaseController;
import com.hengpeng.itfints.tools.PageUtil;
import com.hengpeng.itfints.vo.MemberSession;
/**
 * <pre>
 * 还款控制器
 * </pre>
 * @author weiyuan
 * @version 1.0, 2015年6月3日
 */
@Controller
@RequestMapping("/repayment")
public class RepaymentController extends BaseController
{
	/**
	 * <pre>
	 * 标的还款列表
	 * </pre>
	 * @param map
	 * @param request
	 * @param status 还款计划状态
	 * @param beginDate 还款截止时间查询开始时间
	 * @param endDate 还款截止时间查询结束时间
	 * @param borrowUserName 借款人姓名
	 * @return
	 */
	@RequestMapping(value = "/myRepayMentList")
	public String myRepayMentList(ModelMap map, HttpServletRequest request, String borrowInfoId)
	{
		List<RepaymentPlan> result = serviceFactory.getRepaymentService().getRepaymentPlans(null, Long.parseLong(borrowInfoId), null, null);
		map.put("repaymentList", result);
		return "/page/account/borrowinfo/myRepayMentList.jsp";
	}
	/**
	 * <pre>
	 * 标的还款列表--所有投资标的还款列表
	 * </pre>
	 * @param map
	 * @param request
	 * @param status 还款计划状态
	 * @param beginDate 还款截止时间查询开始时间
	 * @param endDate 还款截止时间查询结束时间
	 * @param borrowUserName 借款人姓名
	 * @return
	 */
	@RequestMapping(value = "/myAllRepayMentList")
	public String myAllRepayMentList(ModelMap map, HttpServletRequest request, String curPage)
	{
		UserInfo user = getSessionMember(request).getUser();
		List<RepaymentPlan> result = serviceFactory.getRepaymentService().getRepaymentPlans(user.getId(), null, null, null);
		int currentPage = !StringHelper.isEmpty(curPage) && curPage !=null ? Integer.parseInt(curPage):1;
		PageUtil pageUtil = new PageUtil(10, result.size(), currentPage);
		map.put("PageUtil", pageUtil);
		map.put("repaymentList", result);
		return "/page/account/borrowinfo/myRepayMentList.jsp";
	}
	/**
	 * <pre>
	 * 收款列表--所有投资标的收款计划
	 * </pre>
	 * @param map
	 * @param request
	 * @param status 还款计划状态
	 * @param beginDate 还款截止时间查询开始时间
	 * @param endDate 还款截止时间查询结束时间
	 * @param borrowUserName 借款人姓名
	 * @return
	 */
	@RequestMapping(value = "/myAllReceivePlanList")
	public String myAllReceivePlanList(ModelMap map, HttpServletRequest request , String curPage)
	{
		UserInfo user = getSessionMember(request).getUser();
		List<ReceivePlan> result = serviceFactory.getRepaymentService().getReceivePlans(null,user.getId(), null, null);
		int currentPage = (!StringHelper.isEmpty(curPage) && curPage != null ? Integer.parseInt(curPage) : 1);
		PageUtil pageUtil = new PageUtil(10, result.size(), currentPage);
		map.put("PageUtil", pageUtil);
		map.put("myReceivePlanList", result);
		return "/page/account/load/index.jsp";
	}
	
	/**
	 * <pre>
	 * 收款列表
	 * </pre>
	 * @param map
	 * @param request
	 * @param status 还款计划状态
	 * @param beginDate 还款截止时间查询开始时间
	 * @param endDate 还款截止时间查询结束时间
	 * @param borrowUserName 借款人姓名
	 * @return
	 */
	@RequestMapping(value = "/myReceivePlanList")
	public String myReceivePlanList(ModelMap map, HttpServletRequest request , String borrowInfoId, String investUserId,String curPage)
	{
		Long bid = null ;
		if (!StringHelper.isEmpty(borrowInfoId)) {
			bid = Long.parseLong(borrowInfoId);
		}
		MemberSession session = getSessionMember(request);
		if (session==null) {
			return "redirect:/page/login/login.jsp";
		}
		UserInfo userInfo = session.getUser();
		List<ReceivePlan> result = serviceFactory.getRepaymentService().getReceivePlans(bid,
				userInfo.getId(), null, null);
		int currentPage = (!StringHelper.isEmpty(curPage) && curPage != null ? Integer.parseInt(curPage) : 1);
		PageUtil pageUtil = new PageUtil(10, result.size(), currentPage);
		map.put("PageUtil", pageUtil);
		map.put("myReceivePlanList", result);
		return "/page/account/investlog/myreceiveplan.jsp";
	}
	/**
	 * <pre>
	 * 获取当前用户的回款日期列表
	 * </pre>
	 * @param request
	 * @param map
	 * @param area
	 * @param gameId
	 * @param printWriter
	 * @throws Exception
	 */
	@RequestMapping(value = "/getReceiveDay", method = RequestMethod.POST)
	@ResponseBody
	public void getReceiveDay (HttpServletRequest request, ModelMap map,String nowYear, String nowMonth,PrintWriter printWriter) throws Exception {
		UserInfo m = getSessionMember(request).getUser();
		List<String> dataList = serviceFactory.getRepaymentService().getReceiveDay(m.getId(), nowYear, nowMonth);
		Gson gson = new Gson();
		printWriter.write(gson.toJson(dataList));
        printWriter.flush();
        printWriter.close();
	}
}
